package com.seckill.rabbitmq;

import com.seckill.redis.RedisService;
import com.seckill.service.GoodsService;
import com.seckill.service.MiaoshaOrderService;
import com.seckill.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yangjunwei
 * @date 2021/11/11 11:28 上午
 */
//@Service
public class MQReceiver {

    public static Logger logger = LoggerFactory.getLogger(MQReceiver.class);

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    MiaoshaOrderService miaoshaOrderService;

    /**
     * 秒杀异步下单
     */
    @RabbitListener(queues = MQConfig.MIAOSHA_QUEUE)
    public void receiveMiaoshaMsg(String msg) {
        logger.info("receiveMiaoshaMsg ==================> {}", msg);
        //接收到信息，开始创建订单
        MiaoshaMessage miaoshaMessage = redisService.stringToBean(msg, MiaoshaMessage.class);
        miaoshaOrderService.doMiaoshaOrder(miaoshaMessage);
    }

    /**
     * 判断订单是否过期
     */
    @RabbitListener(queues = MQConfig.MIAOSHA_ORDER_QUEUE)
    public void receiveMiaoshaOrder(String orderId){
        if(StringUtils.isEmpty(orderId)){
            return;
        }
        orderService.orderDelayCancel(Integer.valueOf(orderId));
    }

//    @RabbitListener(queues = MQConfig.QUEUE)
//    public void receive(String message) {
//        logger.info("receive msg : {}", message);
//    }
//
//    @RabbitListener(queues = MQConfig.TOPIC_QUEUE1)
//    public void receiveTopic1(String message) {
//        logger.info("receiveTopic1 msg : {}", message);
//    }
//
//    @RabbitListener(queues = MQConfig.TOPIC_QUEUE2)
//    public void receiveTopic2(String message) {
//        logger.info("receiveTopic2 msg : {}", message);
//    }
//
//    @RabbitListener(queues = MQConfig.HEADER_QUEUE)
//    public void receiveHeader(byte[] message) {
//        logger.info("receiveHeader msg : " + new String(message));
//    }


}
